package com.xqy.sign;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class DigitalSignature {
    public static void main(String[] args) {
        Map<String,Long> result = rsa(450L);
        Long password = result.get("password");
        Long n = result.get("n");
        Long e = result.get("e");
        Long d = result.get("d");
        System.out.println("password:="+password);
        System.out.println("公钥：=》  n:"+n+";e:"+e);
        System.out.println("私钥：=》  d:"+d+";e:"+e);
        System.out.println(new BigInteger(password+"").
                modPow(new BigInteger(e+""),new BigInteger(n+"")).toString());
        System.out.println("abc".hashCode());
        //password:=8666903
        //n:=15374141
        //e:=2237
        //300
    }
public  static Map<String,Long> rsa(Long context){
    long aLong = Long.parseLong(BigInteger.probablePrime(12, new Random()).toString());
    long bLong = Long.parseLong(BigInteger.probablePrime(12, new Random()).toString());
    long n=aLong*bLong;
    long o=(aLong-1)*(bLong-1);
    long e=Long.parseLong(BigInteger.probablePrime(12, new Random()).toString());;
    long d=0L;
    for(;d<o;d++){
        if ((e*d)%o==1){
            break;
        }
    }
    Long password =Long.
            parseLong(new BigInteger(context+"").
                    modPow(new BigInteger(d+""),new BigInteger(n+"")).toString());
    Map<String,Long> result= new HashMap<String, Long>();
    result.put("password",password);
    result.put("e",e);
    result.put("n",n);
    result.put("d",d);
        return result;
}
}
